package com.xnx3.banmahuishou.wangge.controller;

import java.util.ArrayList;

import java.util.List;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnx3.DateUtil;
import com.xnx3.banmahuishou.agency.vo.QishouListVO;
import com.xnx3.banmahuishou.agency.vo.QishouShowVO;
import com.xnx3.banmahuishou.agency.vo.WanggeListVO;
import com.xnx3.banmahuishou.core.entity.Agency;
import com.xnx3.banmahuishou.core.entity.Qishou;
import com.xnx3.banmahuishou.core.entity.Wangge;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.wangmarket.shop.core.entity.Order;

/**
 * 网格后台 - 围栏管理 
 * @author zxy
 */
@Controller(value="BanmahuishouFenceManagementController")
@RequestMapping("/wangge/fencemanagement/")
public class FenceManagementController extends BaseController {
	@Resource
	private SqlService sqlService;	

	/**
	 * 当前网格的围栏展示
	 * @author zxy
	 */
	@RequestMapping("guanli${url.suffix}")
	public String guanli(HttpServletRequest request,Model model) {
		//获取当前用户id
		int id = getUserId();
		//根据id 查取该网格的记录
		Wangge wangge = sqlService.findById(Wangge.class, id);
		if (wangge == null) {
			model.addAttribute("msg", 1);;
		}
		//查询该网格的代理商区域
		String sql = "SELECT * FROM laji_agency WHERE userid = " + wangge.getDailishangUserid();
		List<Agency> list = sqlService.findBySqlQuery(sql, Agency.class);
		StringBuffer stb = new StringBuffer();
		for (Agency agency : list) {
			// 判断该代理商的代理区域 省代理
			if (ObjectUtils.equals(agency.getShi(), "全部")) {
				stb.append(agency.getSheng() + ",");
			}
			//区代理
			if (!ObjectUtils.equals(agency.getQu(), "全部")) {
				stb.append(agency.getQu() + ",");
			}
			//市代理
			if (ObjectUtils.equals(agency.getQu(), "全部") && !ObjectUtils.equals(agency.getShi(), "全部")) {
				stb.append(agency.getShi() + ",");
			}
			String area = null;
			if ( stb.length() > 1) {
				area = stb.substring( 0 , stb.length() - 1 );
			}
			model.addAttribute( "area" , area );	
		}
		//查询该网格的区域 如果存在 展示 如果不存在 提示信息
		if (wangge.getServiceArea() == null ) {
			model.addAttribute("msg", 2);
		}
		//日志管理
		ActionLogUtil.insert( request ,  "网格id为" + id + "访问了网格管理" );
		//电子围栏展示页面
		return "/wangge/fencemanagement/guanli";
	}
	
	
	/**
	 * 获取当前登录用户自己所能看的网格电子围栏的数据信息
	 * @author zxy
	 * @return 围栏信息
	 */
	@ResponseBody
	@RequestMapping(value = "getWanggeWg.json",method = {RequestMethod.POST})
	public WanggeListVO getWanggeWg(HttpServletRequest request,Model model) {
		WanggeListVO vo = new WanggeListVO();
		int id = getUserId();	//要查看的网格的id
		List<Wangge> list = sqlService.findBySqlQuery("SELECT * FROM laji_wangge WHERE id = " + id + " AND state = " + Wangge.STATE_NORMAL, Wangge.class);
		vo.setList(list);
		//日志记录
		ActionLogUtil.insert( request , "网格id为" + id + "获取了网格电子围栏信息" );
		return vo;
	}
	
	
	/**
	 * 获取当前用户代理区域下电子网格的骑手数据
	 * @author zxy
	 * @return 电子网格数据
	 */
	@ResponseBody
	@RequestMapping(value = "getQishou.json",method = {RequestMethod.POST})
	public QishouListVO getQishou(HttpServletRequest request,Model model) {
		QishouListVO vo = new QishouListVO();
		List<QishouShowVO> showList = new ArrayList<QishouShowVO>();
		int wangeUserid = getUserId();	//要查看的网格的userid
		//取出当前网格下的工作状态正常的骑手集合
		String sql = "SELECT * FROM laji_qishou WHERE wangge_userid = " + wangeUserid + " AND state = " + Qishou.STATE_NORMAL ;
		List<Qishou> list = sqlService.findBySqlQuery( sql, Qishou.class);
		if (!list.isEmpty()) {
			for (Qishou qishou : list) {
				QishouShowVO showvo = new QishouShowVO();
				showvo.setLatitude(qishou.getLatitude());
				showvo.setLongitude(qishou.getLongitude());
				showvo.setQishouid(qishou.getId());
				int count = sqlService.count("shop_order", " WHERE qishou_id=" + qishou.getId() +  " AND state = " + Order.STATE_DAIQUHUO + " AND reserve_end_time >= " + DateUtil.timeForUnix10());
				showvo.setCount(count);
				showvo.setQishouName(qishou.getName());
				showvo.setWanggeName(qishou.getWanggeUsername());
				showList.add(showvo);
			}
		}
		vo.setList(showList); 
		//日志记录
		ActionLogUtil.insert( request , "id为" + wangeUserid + "的网格查看了网格下的骑手数据" );
		return vo;
	}
}
